
#include "stdafx.h"
#include "ConstDef.h"
#include "ReloadCode.h"
#include "ReloadApp.h"
#include "ReloadPacket.h"

namespace reload
{
	ChordUpdate::ChordUpdate()
	:uptime_(0),type_(ChordUpdateType_Reserved)
	{

	}

	ChordUpdate::~ChordUpdate()
	{
		uptime_ = 0;
		type_ = ChordUpdateType_Reserved;
	}

	retT ChordUpdate::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(uptime_));
		R(_pBuffer->ntohRead(type_));
		switch(type_)
		{
		case ChordUpdateType_PeerReady:break;
		case ChordUpdateType_Neighbors:R(predecessors_.readFromBuffer(_pBuffer));R(successors_.readFromBuffer(_pBuffer));break;
		case ChordUpdateType_Full:R(predecessors_.readFromBuffer(_pBuffer));R(successors_.readFromBuffer(_pBuffer));R(fingers_.readFromBuffer(_pBuffer));break;
		default:RET(INFO_PROTOCOL_RELOAD_CHORDUPDATE_UNKNOWN_TYPE);
		}
		READFROMBUFFER_END;
	}

	retT ChordUpdate::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(uptime_));
		R(_pBuffer->htonWrite(type_));
		switch(type_)
		{
		case ChordUpdateType_PeerReady:break;
		case ChordUpdateType_Neighbors:R(predecessors_.writeToBuffer(_pBuffer));R(successors_.writeToBuffer(_pBuffer));break;
		case ChordUpdateType_Full:R(predecessors_.writeToBuffer(_pBuffer));R(successors_.writeToBuffer(_pBuffer));R(fingers_.writeToBuffer(_pBuffer));break;
		default:RET(INFO_PROTOCOL_RELOAD_CHORDUPDATE_UNKNOWN_TYPE);
		}
		WRITETOBUFFER_END;
	}

	std::size_t ChordUpdate::size()
	{
		std::size_t totalSize = 0;
		totalSize += sizeof(uptime_);
		totalSize += sizeof(type_);
		switch(type_)
		{
		case ChordUpdateType_PeerReady:break;
		case ChordUpdateType_Neighbors:totalSize += predecessors_.size();totalSize += successors_.size();break;
		case ChordUpdateType_Full:totalSize += predecessors_.size();totalSize += successors_.size();totalSize += fingers_.size();break;
		default:break;
		}
		return totalSize;
	}

	retT ChordUpdate::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(predecessors_.attachRootElement(_pRootElement));
		R(successors_.attachRootElement(_pRootElement));
		R(fingers_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	ChordRouteQueryAns::ChordRouteQueryAns()
	{

	}

	ChordRouteQueryAns::~ChordRouteQueryAns()
	{

	}

	retT ChordRouteQueryAns::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(nextPeer_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT ChordRouteQueryAns::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(nextPeer_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t ChordRouteQueryAns::size()
	{
		return nextPeer_.size();
	}

	retT ChordRouteQueryAns::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(nextPeer_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	ChordLeaveData::ChordLeaveData()
	:type_(ChordLeaveType_Reserved)
	{

	}

	ChordLeaveData::~ChordLeaveData()
	{
		type_ = ChordLeaveType_Reserved;
	}

	retT ChordLeaveData::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(type_));
		switch(type_)
		{
		case ChordLeaveType_FromSucc:R(successors_.readFromBuffer(_pBuffer));break;
		case ChordLeaveType_FromPred:R(predecessors_.readFromBuffer(_pBuffer));break;
		default:RET(INFO_PROTOCOL_RELOAD_CHORDLEAVEDATA_UNKNOWN_TYPE);
		}
		READFROMBUFFER_END;
	}

	retT ChordLeaveData::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(type_));
		switch(type_)
		{
		case ChordLeaveType_FromSucc:R(successors_.writeToBuffer(_pBuffer));break;
		case ChordLeaveType_FromPred:R(predecessors_.writeToBuffer(_pBuffer));break;
		default:RET(INFO_PROTOCOL_RELOAD_CHORDLEAVEDATA_UNKNOWN_TYPE);
		}
		WRITETOBUFFER_END;
	}

	std::size_t ChordLeaveData::size()
	{
		std::size_t totalSize = 0;
		totalSize += sizeof(type_);
		switch(type_)
		{
		case ChordLeaveType_FromSucc:totalSize += successors_.size();break;
		case ChordLeaveType_FromPred:totalSize += predecessors_.size();break;
		default:break;
		}
		return totalSize;
	}

	retT ChordLeaveData::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(successors_.attachRootElement(_pRootElement));
		R(predecessors_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}
} /*namespace reload*/
